package auto.app.service.impl;

import auto.app.controller.from.DatabaseConnect.DatabaseConnectionAdd;
import auto.app.controller.from.DatabaseConnect.DatabaseConnectionUpdate;
import auto.app.controller.info.ConnecitonInfo.ConnectionConfiguraDetailInfo;
import auto.app.exetion.MainExection;
import auto.app.model.CaseInterface;
import auto.app.model.ConnectionConfiguration;
import auto.app.model.DatabaseConnect;
import auto.app.dao.DatabaseConnectDao;
import auto.app.service.CaseService;
import auto.app.service.ConnectionConfigurationService;
import auto.app.service.DatabaseConnectService;
import auto.app.util.CommonUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

import static auto.app.exetion.MainExetionMessage.*;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 杨森
 * @since 2022-10-17
 */
@Service
public class DatabaseConnectServiceImpl extends ServiceImpl<DatabaseConnectDao, DatabaseConnect> implements DatabaseConnectService {
    @Autowired
    ConnectionConfigurationService connectionConfigurationService;
    @Autowired
    CaseService caseService;

    @Override
    public Boolean addDatabaseConnect(DatabaseConnectionAdd databaseConnectionAdd) throws MainExection {
        //如果没传数据库配置id使用默认
        if (ObjectUtil.isEmpty(databaseConnectionAdd.getConnectionId())){
            LambdaQueryWrapper<ConnectionConfiguration> lambdaQueryWrapper=new LambdaQueryWrapper<>();
            lambdaQueryWrapper.eq(ConnectionConfiguration::getConnectionDefault,1);
            ConnectionConfiguration configurationServiceOne = connectionConfigurationService.getOne(lambdaQueryWrapper);
            if (ObjectUtil.isNull(configurationServiceOne)){
                throw new MainExection(DEFALUTCONNECTFONFIGURATION.getCode(),DEFALUTCONNECTFONFIGURATION.getMsg());
            }
            databaseConnectionAdd.setConnectionId(configurationServiceOne.getConnectionId());
        }else{
            ConnectionConfiguration configurationServiceById = connectionConfigurationService.getById(databaseConnectionAdd.getConnectionId());
            if (ObjectUtil.isNull(configurationServiceById)){
                throw new MainExection(CONNECTIONSLECTERROR.getCode(),CONNECTIONSLECTERROR.getMsg());
            }
        }
        //查询测试用例是否存在
        CaseInterface caseServiceById = caseService.getById(databaseConnectionAdd.getCaseId());
        if (ObjectUtil.isNull(caseServiceById)){
            throw new MainExection(CASEERROR.getCode(),CASEERROR.getMsg());
        }
        DatabaseConnect databaseConnect = CommonUtil.copyOne(databaseConnectionAdd, DatabaseConnect.class);
        databaseConnect.setCreateTime(LocalDateTime.now());
        return baseMapper.insert(databaseConnect) > 0;
    }

    @Override
    public Boolean deleteDatabaseConnect(String databaseId) throws MainExection {
        DatabaseConnect databaseConnect = baseMapper.selectById(databaseId);
        if (ObjectUtil.isNull(databaseConnect)){
            throw new MainExection(DATABASECAOZUOERROR.getCode(),DATABASECAOZUOERROR.getMsg());
        }
        return baseMapper.deleteById(databaseId)>0;
    }

    @Override
    public Boolean updateDatabaseConnect(DatabaseConnectionUpdate databaseConnectionUpdate) throws MainExection {
        //如果没传数据库配置id使用默认
        if (ObjectUtil.isEmpty(databaseConnectionUpdate.getConnectionId())){
            LambdaQueryWrapper<ConnectionConfiguration> lambdaQueryWrapper=new LambdaQueryWrapper<>();
            lambdaQueryWrapper.eq(ConnectionConfiguration::getConnectionDefault,1);
            ConnectionConfiguration configurationServiceOne = connectionConfigurationService.getOne(lambdaQueryWrapper);
            if (ObjectUtil.isNull(configurationServiceOne)){
                throw new MainExection(DEFALUTCONNECTFONFIGURATION.getCode(),DEFALUTCONNECTFONFIGURATION.getMsg());
            }
            databaseConnectionUpdate.setConnectionId(configurationServiceOne.getConnectionId());
        }else{
            ConnectionConfiguration configurationServiceById = connectionConfigurationService.getById(databaseConnectionUpdate.getConnectionId());
            if (ObjectUtil.isNull(configurationServiceById)){
                throw new MainExection(CONNECTIONSLECTERROR.getCode(),CONNECTIONSLECTERROR.getMsg());
            }
        }
        DatabaseConnect databaseConnect = CommonUtil.copyOne(databaseConnectionUpdate, DatabaseConnect.class);
        databaseConnect.setUpdateTime(LocalDateTime.now());
        return baseMapper.updateById(databaseConnect) >0;
    }

    @Override
    public List<DatabaseConnect> listDatabaseConnect(Integer caseId) {
        LambdaQueryWrapper<DatabaseConnect> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(DatabaseConnect::getCaseId,caseId);
        List<DatabaseConnect> databaseConnects = baseMapper.selectList(lambdaQueryWrapper);
        return ObjectUtil.isNotNull(databaseConnects)? databaseConnects:new ArrayList<>();
    }

    @Override
    public Boolean stateDatabaseConnect(String databaseId) throws MainExection {
        DatabaseConnect databaseConnect = baseMapper.selectById(databaseId);
        if (ObjectUtil.isNull(databaseConnect)){
            throw new MainExection(DATABASECAOZUOERROR.getCode(),DATABASECAOZUOERROR.getMsg());
        }
        if (databaseConnect.getDatabaseState()==0)databaseConnect.setDatabaseState(1);
        else databaseConnect.setDatabaseState(0);
        return baseMapper.updateById(databaseConnect)>0;
    }

    //查询启用的数据库连接类
    @Override
    public List<DatabaseConnect> listDatabaseConnectState(Integer caseId) {
        LambdaQueryWrapper<DatabaseConnect> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(DatabaseConnect::getCaseId,caseId)
                .eq(DatabaseConnect::getDatabaseState,1);
        List<DatabaseConnect> databaseConnects = baseMapper.selectList(lambdaQueryWrapper);
        return databaseConnects.size()>0 && ObjectUtil.isNotNull(databaseConnects)?databaseConnects:new ArrayList<>();
    }


}
